﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.MsQuestion;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Ccit.OA.Question
{
    public partial class qst_manage_detail : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            _hidRecid.Value = (null != Request.QueryString["Id"]) ? Request.QueryString["Id"].ToString() : "";
            _hidQuenID.Value = (Request.QueryString["QId"] != "0") ? Request.QueryString["QId"].ToString() : "";
            InitPage();
        }
        private void InitPage()
        {
            QuestionBLL Bll = new QuestionBLL();
            if (_hidRecid.Value != "")
            { 
                DataSet dSet = Bll.GetQuestion(_hidRecid.Value.ToString(), "1");
                int iLine = 1;
                if (dSet.Tables[0].Rows.Count > 0)
                {
                    txtTitle.Text = dSet.Tables[0].Rows[0]["Qst_A1_20"].ToString();
                    txtDescribe.Text = dSet.Tables[0].Rows[0]["Qst_A2_30"].ToString();
                    radioType.SelectedValue = dSet.Tables[0].Rows[0]["Qst_A2_50"].ToString();
                    if (dSet.Tables[0].Rows[0]["Qst_A2_60"].ToString() == "1")
                    {
                        chkOpinion.Checked = true;
                    }
                    txtNumber.Text = dSet.Tables[0].Rows[0]["Qst_A2_70"].ToString();
                    txtOrder.Text = dSet.Tables[0].Rows[0]["Qst_A2_40"].ToString();

                    for (int i = 0; i < dSet.Tables[0].Rows.Count; i++)
                    {
                        DataRow iRow = dSet.Tables[0].Rows[i];
                        CreateTable(iLine, iRow);
                        iLine++;
                    }
                }
            }
            else
            {
                if (_hidQuenID.Value != "")
                {
                    DataSet dSet = Bll.GetQuestion(_hidQuenID.Value.ToString(), "0");
                    if (dSet.Tables[0].Rows.Count > 0)
                    {
                        txtTitle.Text = dSet.Tables[0].Rows[0]["Qst_A1_20"].ToString();
                    }
                }
            }
            Bll = null;
        }

        private void CreateTable(int iLine, DataRow iRow)
        {
            HtmlTableRow oRow = new HtmlTableRow();
            oRow.ID = "row_" + iLine.ToString();
            oRow.Attributes["class"] = "listrow2";

            HtmlTableCell oCellM = new HtmlTableCell();
            oCellM.Align = "center";
            oCellM.Attributes["class"] = "listcellrow";
            oCellM.Attributes["width"] = "8%";
            oCellM.InnerText = iLine.ToString();
            oRow.Cells.Add(oCellM);

            HtmlTableCell oCellX = new HtmlTableCell();
            oCellX.Align = "center";
            oCellX.Attributes["class"] = "listcellrow";
            oCellX.Attributes["width"] = "30%";
            oCellX.InnerHtml = "<input type='text' id='txtNAME_" + iLine + "' class='input' value=\"" + iRow["Qst_A3_40"].ToString().Replace("\"", "&quot;").Replace("\'", char.ConvertFromUtf32(39)) + "\" style='width:99%'>";
            oRow.Cells.Add(oCellX);

            ////Operate 操作
            HtmlTableCell oCellDel = new HtmlTableCell();
            oCellDel.Align = "center";
            oCellDel.Attributes["class"] = "listcellrow";
            oCellDel.Attributes["width"] = "10%";
            oCellDel.InnerHtml = "<a href=\"javascript:doRemoveRows('tbProduct'," + iRow["Qst_A3_10"].ToString() + ",'" + oRow.ID + "')\" class='linktext'>";
            oCellDel.InnerHtml += "删除</a>";
            oRow.Cells.Add(oCellDel);

            tbProduct.Rows.Add(oRow); 
        }
        #region Ajax Methods
        [global::System.Web.Services.WebMethod]
        public static string DoSave(string strJson, string cType)
        {
            QuestionBLL Bll = new QuestionBLL();
            MsQuentions ms = (MsQuentions)JavaScriptConvert.DeserializeObject(strJson, typeof(MsQuentions));
            List<string> lstResult = Bll.DoSaveQuestions(ms);
            var newObj = new { result = lstResult[0], message = lstResult[1], ctrtype = cType };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            Bll = null;
            newObj = null;
            return serExpr;
        }
        [global::System.Web.Services.WebMethod]
        public static string DoOperate(string sRecid, string cType)
        {
            QuestionBLL Bll = new QuestionBLL();
            List<string> lstResult = Bll.DoOperateTions(sRecid, "1");
            var newObj = new { result = lstResult[0], message = lstResult[1], ctrtype = cType };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            Bll = null;
            newObj = null;
            return serExpr;
        }
        #endregion
    }
}
